package com.gl.battleship.services.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gl.battleship.domain.Notification;
import com.gl.battleship.domain.UserState;
import com.gl.battleship.domain.exceptions.PlayerException;
import com.gl.battleship.domain.response.BattleshipResponse;
import com.gl.battleship.services.IBattleshipServices;
import com.gl.battleship.services.exceptions.GameException;
import com.gl.battleship.services.exceptions.NotificationException;
import com.gl.battleship.services.exceptions.UserException;

public class RestBattleshipServices implements IRestBattleshipServices {

	@Autowired
	IBattleshipServices battleshipServices;

	@RequestMapping("/login/{username}/{password}")
	@ResponseBody
	public BattleshipResponse login(@PathVariable("username") String user,
			@PathVariable("password") String pass) {

		BattleshipResponse response = new BattleshipResponse();

		try {
			String data = battleshipServices.login(user, pass);
			response.setStatus("SUCCESS");
			response.setData(data);

		} catch (UserException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/logout/{securityToken}")
	@ResponseBody
	public BattleshipResponse logout(@PathVariable("securityToken") String securityToken) {

		BattleshipResponse response = new BattleshipResponse();
		
		try {

			String data =battleshipServices.logOut(securityToken);
			response.setStatus("SUCCESS");
			response.setData(data);

		} catch (UserException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
			
		} catch (GameException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	
	@RequestMapping("/getUsersByState/{securityToken}")
	@ResponseBody
	public BattleshipResponse getUsersByState(@PathVariable("securityToken") String securityToken) {

		BattleshipResponse response = new BattleshipResponse();
		response = getUsersByState(securityToken, "");
		return response;

	}

	@RequestMapping("/getUsersByState/{securityToken}/{userState}")
	@ResponseBody
	public BattleshipResponse getUsersByState(@PathVariable("securityToken") String securityToken,@PathVariable("userState") String userState) {

			BattleshipResponse response = new BattleshipResponse();
			List<String> data ;
			response.setStatus("SUCCESS");

		try {

			if (userState.equalsIgnoreCase(UserState.ALL.toString()))
				data = battleshipServices.getUsersByState(securityToken,
						UserState.ALL);
			if (userState.equalsIgnoreCase(UserState.AVAILABLE.toString()))
				data = battleshipServices.getUsersByState(securityToken,
						UserState.AVAILABLE);
			if (userState.equalsIgnoreCase(UserState.NOT_AVAILABLE.toString())) {
				data = battleshipServices.getUsersByState(securityToken,
						UserState.NOT_AVAILABLE);
			} else {
				data = battleshipServices.getUsersByState(securityToken,UserState.ALL);
			}
			response.setData(data);

		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}

		return response;
	}

	@RequestMapping("/declareWar/{securityToken}/{opponent}")
	@ResponseBody
	public BattleshipResponse declareWar(@PathVariable("securityToken") String securityToken,@PathVariable("opponent") String opponent) {

		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");
		try {
			data= battleshipServices.declareWar(securityToken, opponent);
			response.setData(data);
		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		} catch (PlayerException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (GameException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/acceptWar/{securityToken}/{warToken}")
	@ResponseBody
	public BattleshipResponse acceptWar(@PathVariable("securityToken") String securityToken,@PathVariable("warToken") String warToken) {

		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");
		try {

			data= battleshipServices.acceptWar(securityToken, warToken);
			response.setData(data);

		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (GameException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (PlayerException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/refuseWar/{securityToken}/{warToken}")
	@ResponseBody
	public BattleshipResponse refuseWar(@PathVariable("securityToken") String securityToken,@PathVariable("warToken") String warToken) {
		
		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");
		try {

			data= battleshipServices.refuseWar(securityToken, warToken);
			response.setData(data);

		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (GameException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (PlayerException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}
	
	
	@RequestMapping("/retiredWar/{nick}")
	@ResponseBody
	public BattleshipResponse retiredWar(@PathVariable("nick") String nick){
		
		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");
	
		try {
			data= battleshipServices.retiredWar(nick);
			response.setData(data);
		} catch (GameException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		
		return response;
	
		
	}
	

	@RequestMapping("/validateFleet/{securityToken}/{warToken}/{fleet}")
	@ResponseBody
	public BattleshipResponse validateFleet(@PathVariable("securityToken") String securityToken,@PathVariable("warToken") String warToken,@PathVariable("fleet") String fleet) {

		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");
		try {
			data=battleshipServices.validateFleet(securityToken, warToken,fleet);
			response.setData(data);

		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (GameException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (PlayerException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/shoot/{securityToken}/{warToken}/{x}/{y}")
	@ResponseBody
	public BattleshipResponse shoot(@PathVariable("securityToken") String securityToken,@PathVariable("warToken") String warToken,@PathVariable("x") int x, @PathVariable("y") String y) {
		
		BattleshipResponse response = new BattleshipResponse();
		String data ;
		response.setStatus("SUCCESS");

		try {

			data=battleshipServices.shoot(securityToken, warToken, x, y);
			response.setData(data);

		} catch (UserException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (GameException e) {

			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());

		} catch (PlayerException e) {
			
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/getNotification/{nick}")
	@ResponseBody
	public BattleshipResponse getNotification(@PathVariable("nick") String nick) {
		
		BattleshipResponse response = new BattleshipResponse();
		List <Notification> data ;
		response.setStatus("SUCCESS");

		try {
			data= battleshipServices.getNotification(nick);
			response.setData(data);
		} catch (UserException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		} catch (NotificationException e) {
			response.setStatus("ERROR");
			response.setErrorMsg(e.getMessage());
		}

		return response;

	}

	

}
